<template>
	<el-dialog :title="dialogTitle" :visible.sync="visible" top="0" width="560px" append-to-body>
		<el-form v-bind="formConfig" :model="formData" :rules="formRules" label-width="80px" v-loading="loading">
			<el-row :gutter="16">
				<el-col :span="24">
					<el-form-item label="分类名称" prop="name">
						<el-input v-model="formData.name" placeholder="请输入分类名称" maxlength="30" show-word-limit></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="分类类型" prop="labelType">
						<el-select v-model="formData.labelType" placeholder="请选择分类类型" style="width: 100%">
							<el-option v-for="(item, index) in options" :key="index" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="排序" prop="orderBy">
						<el-input-number v-model="formData.orderBy" controls-position="right" :min="1"></el-input-number>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template slot="footer">
			<el-button size="medium" @click="visible = false">取消</el-button>
			<el-button size="medium" type="primary" :loading="loading" @click="handleSubmitForm()">{{ formTypeText }}</el-button>
		</template>
	</el-dialog>
</template>

<script>
import moment from 'moment';
import { mapGetters } from 'vuex';
import formMixins from '@/mixins/formMixins';
import { queryDetail, addNotice, updateNotice } from '@/api/basedata/sysNotice';
import { queryPageList, addNoticeFile, deleteNoticeFile } from '@/api/basedata/sysNoticeFiles';
export default {
	mixins: [formMixins],
	data() {
		return {
			formName: '通知公告',
			formRules: {
				title: [{ required: true, message: '请输入公告标题', trigger: 'blur' }],
				author: [{ required: true, message: '请输入公告作者', trigger: 'blur' }],
				pudDate: [{ required: true, message: '请选择发布时间', trigger: 'change' }],
				content: [{ required: true, message: '请输入公告内容', trigger: 'blur' }]
			}
		};
	},
	computed: {
		...mapGetters(['userInfo']),
		detailData() {
			return eval(queryDetail);
		},
		callFunction() {
			return eval(this.addOrEdit ? addNotice : updateNotice);
		},
		submitParams() {
			let params = { ...this.formData };
			let files = params.sysNoticeFilesList;
			if (this.addOrEdit) {
				params['type'] = 1;
				params['isShow'] = 0;
				params['orderBy'] = params.orderBy || 10;
				params['sysNoticeFilesList'] = files.map(n => ({ fileId: n.id }));
			} else {
				let addFiles = files.filter(n => !n.fileId);
				params['sysNoticeFilesList'] = addFiles.map(n => ({ fileId: n.id }));
			}
			return params;
		}
	},
	methods: {
		formatFormParams() {
			if (!this.addOrEdit) {
				this.getNoticeFilesList();
			} else {
				this.$set(this.formData, 'author', this.userInfo.username);
				this.$set(this.formData, 'pudDate', moment().format('YYYY-MM-DD HH:mm:ss'));
			}
		},

		//获取通知公告附件
		async getNoticeFilesList() {
			let params = { page: 1, limit: 9999, noticeId: this.dataId };
			let result = await queryPageList(params).then(res => res.data);
			if (result.code === '0') {
				this.$set(this.formData, 'sysNoticeFilesList', result.data || []);
			}
		},

		//提交请求之前
		async beforeSubmitForm() {
			let uploadFiles = this.submitParams?.sysNoticeFilesList || [];
			return Promise.all(uploadFiles.map(n => this.addNoticeFileRequest(n)));
		},

		//添加通知公告附件
		async addNoticeFileRequest(data) {
			let params = { noticeId: this.dataId, noticeType: 1, ...data };
			let result = await addNoticeFile(params).then(res => res.data);
			return Promise.resolve(result);
		},

		//删除通知公告附件
		async handleRemoveFile(data) {
			await deleteNoticeFile(data.id);
		}
	}
};
</script>
